#!/usr/bin/env bash

set -e
set -o pipefail

# Avoid dependency on locale
LC_ALL=C

PKG_ROOT='root'

DEB_ARCH=${DEB_ARCH:-amd64}
SANITIZER=${SANITIZER:-""}
SOURCE=${SOURCE:-$PKG_ROOT}

cd "$(dirname "${BASH_SOURCE[0]}")"

HELP="${0} [--test] [--rpm] [-h|--help]
  --test - adds '+test' prefix to version
  --apk - build APK packages
  --archlinux - build archlinux packages
  --rpm - build RPM packages
  --tgz - build tarball package
  --deb - build deb package
  --help - show this help and exit

Used envs:
  DEB_ARCH='${DEB_ARCH}'
  OUTPUT_DIR='${OUTPUT_DIR}' - where the artifact will be placed
  SANITIZER='${SANITIZER}' - if any sanitizer is used, affects version string
  SOURCE='${SOURCE}' - directory with sources tree
  VERSION_STRING='${VERSION_STRING}' - the package version to overwrite
"

CLICKHOUSE_VERSION_STRING=${VERSION_STRING}
export CLICKHOUSE_VERSION_STRING


while [[ $1 == --* ]]
do
    case "$1" in
        --test )
            VERSION_POSTFIX+='+test'
            shift ;;
        --deb )
            MAKE_DEB=1
            shift ;;
        --apk )
            MAKE_APK=1
            shift ;;
        --archlinux )
            MAKE_ARCHLINUX=1
            shift ;;
        --rpm )
            MAKE_RPM=1
            shift ;;
        --tgz )
            MAKE_TGZ=1
            shift ;;
        --help )
            echo "$HELP"
            exit ;;
        * )
            echo "Unknown option $1"
            exit 2 ;;
    esac
done

function deb2tgz {
    local FILE PKG_NAME PKG_DIR PKG_PATH TARBALL
    FILE=$1
    PKG_NAME=${FILE##*/}; PKG_NAME=${PKG_NAME%%_*}
    PKG_DIR="$PKG_NAME-$CLICKHOUSE_VERSION_STRING"
    PKG_PATH="$OUTPUT_DIR/$PKG_DIR"
    TARBALL="$OUTPUT_DIR/$PKG_DIR-$DEB_ARCH.tgz"
    rm -rf "$PKG_PATH"
    dpkg-deb -R "$FILE" "$PKG_PATH"
    mkdir -p "$PKG_PATH/install"
    cat > "$PKG_PATH/install/doinst.sh" << 'EOF'
#!/bin/sh
set -e

SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
for filepath in `find $SCRIPTPATH/.. -type f -or -type l | grep -v "\.\./install/"`; do
    destpath=${filepath##$SCRIPTPATH/..}
    mkdir -p $(dirname "$destpath")
    cp -r "$filepath" "$destpath"
done
EOF
    chmod +x "$PKG_PATH/install/doinst.sh"
    if [ -f "$PKG_PATH/DEBIAN/postinst" ]; then
        # we don't need debconf source in doinst in any case
        tail +2 "$PKG_PATH/DEBIAN/postinst" | grep -v debconf/confmodule >> "$PKG_PATH/install/doinst.sh"
    fi
    rm -rf "$PKG_PATH/DEBIAN"
    if [ -f "/usr/bin/pigz" ]; then
        tar --use-compress-program=pigz -cf "$TARBALL" -C "$OUTPUT_DIR" "$PKG_DIR"
    else
        tar -czf "$TARBALL" -C "$OUTPUT_DIR" "$PKG_DIR"
    fi

    # Cut the $OUTPUT_DIR/ from the sha512sum output to make it universal
    sha512sum "$TARBALL" | sed "s|$OUTPUT_DIR/||" > "$TARBALL".sha512

    rm -r "$PKG_PATH"
}

# Build options
if [ -n "$SANITIZER" ]; then
    if [[ "$SANITIZER" == "address" ]]; then VERSION_POSTFIX+="+asan"
    elif [[ "$SANITIZER" == "thread" ]]; then VERSION_POSTFIX+="+tsan"
    elif [[ "$SANITIZER" == "memory" ]]; then VERSION_POSTFIX+="+msan"
    elif [[ "$SANITIZER" == "undefined" ]]; then VERSION_POSTFIX+="+ubsan"
    else
        echo "Unknown value of SANITIZER variable: $SANITIZER"
        exit 3
    fi
elif [[ $BUILD_TYPE == 'debug' ]]; then
    VERSION_POSTFIX+="+debug"
elif [[ $BUILD_TYPE =~ 'coverage' ]]; then
    VERSION_POSTFIX+="+coverage"
fi

if [[ "$PKG_ROOT" != "$SOURCE" ]]; then
    # packages are built only from PKG_SOURCE
    rm -rf "./$PKG_ROOT"
    ln -sf "$SOURCE" "$PKG_SOURCE"
fi

CLICKHOUSE_VERSION_STRING+=$VERSION_POSTFIX
echo -e "\nCurrent version is $CLICKHOUSE_VERSION_STRING"

for config in clickhouse*.yaml; do
    if [ -n "$MAKE_DEB" ] || [ -n "$MAKE_TGZ" ]; then
        echo "Building deb package for $config"
        PKG_PATH=$(nfpm package --target "$OUTPUT_DIR" --config "$config" --packager deb | tee /dev/stderr | grep "created package:" | sed 's/.*created package: //')
    fi

    if [ -n "$MAKE_APK" ]; then
        echo "Building apk package for $config"
        nfpm package --target "$OUTPUT_DIR" --config "$config" --packager apk
    fi

    if [ -n "$MAKE_ARCHLINUX" ]; then
        echo "Building archlinux package for $config"
        nfpm package --target "$OUTPUT_DIR" --config "$config" --packager archlinux
    fi

    if [ -n "$MAKE_RPM" ]; then
        echo "Building rpm package for $config"
        nfpm package --target "$OUTPUT_DIR" --config "$config" --packager rpm
    fi

    if [ -n "$MAKE_TGZ" ]; then
        echo "Building tarball for $config"
        deb2tgz "$PKG_PATH"
    fi
done

# vim: ts=4: sw=4: sts=4: expandtab
